<template>
  <div class="goodsGuanIndex">
    <el-card class="box-card">
      <!-- one------------------------------------------------------- -->
      <div slot="header" class="clearfix header">
        <span>请输入商品名</span>
        <el-button type="primary" round @click="createFn">创建</el-button>
      </div>
      <!-- -------------------------------------------------------------- -->

      <!-- two 搜索框-->
      <div class="search">
        <el-input
          placeholder="请输入内容"
          v-model="val"
          class="input-with-select"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="searchFn"
          ></el-button>
        </el-input>

        <!-- ----------------------------------------------------------- -->

        <!-- three----------日期 -->
        <el-date-picker
          v-model="data"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </div>

      <!-- --------------------------------------------------- -->

      <!-- four--------------------------------------------- -->
      <el-table :data="tableData" style="width: 100%">
        <!-- 第一栏 -->
        <el-table-column prop="goods_id" label="编号" width="180">
        </el-table-column>

        <!-- 第二栏 -->
        <el-table-column
          prop="cat_name"
          label="分类名称"
          width="180"
        ></el-table-column>

        <!-- 第三栏 -->
        <el-table-column prop="goods_img" label="图片">
          <template slot-scope="">
            <img src="../../../assets/logo.png" width="80" height="80" />
          </template>
        </el-table-column>

        <!-- 第四栏 -->
        <el-table-column prop="goods_name" label="标题"></el-table-column>

        <!-- 第五栏 -->
        <el-table-column prop="goods_number" label="库存"> </el-table-column>

        <!-- 第六栏 -->
        <el-table-column prop="market_price" label="价格"> </el-table-column>

        <!-- 第七栏 -->
        <el-table-column prop="shop_price" label="促销价"> </el-table-column>

        <!-- 第八栏 -->
        <el-table-column prop="add_time" label="创建于"> </el-table-column>

        <!-- 第九栏 -->
        <el-table-column prop="is_best" label="上架" width="60">
          <template slot-scope="item">
            <el-switch
              @change="changeFn($event, item.row, 'is_best')"
              active-value="1"
              inactive-value="0"
              v-model="item.row.is_best"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>
        <!-- 第十栏 -->
        <el-table-column prop="is_hot" label="热卖" width="60">
          <template slot-scope="item">
            <el-switch
              @change="changeFn($event, item.row, 'is_hot')"
              active-value="1"
              inactive-value="0"
              v-model="item.row.is_hot"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>

        <!-- 第十一栏 -->
        <el-table-column prop="is_new" label="推荐" width="60">
          <template slot-scope="item">
            <el-switch
              @change="changeFn($event, item.row, 'is_new')"
              active-value="1"
              inactive-value="0"
              v-model="item.row.is_new"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>

        <!-- 第十二栏 -->
        <el-table-column prop="is_sell" label="新品" width="60">
          <template slot-scope="item">
            <el-switch
              @change="changeFn($event, item.row, 'is_sell')"
              active-value="1"
              inactive-value="0"
              v-model="item.row.is_sell"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>
      </el-table>
      <!----------------------------------------------------------------------  -->

      <!-- five----编辑弹框------------------------------------------------------------ -->
      <el-dialog
        title="编辑商品类型"
        :visible.sync="isShowEditTk"
        width="30%"
        :before-close="handleClose"
      >
        <!-- form -->
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="类型名称" prop="type_name">
            <el-input
              type="password"
              v-model="ruleForm.type_name"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <!-- /form -->
        <span>这是一段信息</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="isShowEditTk = false">取 消</el-button>
          <el-button type="primary" @click="isShowEditTk = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- --------------------------------------------------------------------------------------- -->
    </el-card>
  </div>
</template>
<script>
import { getGoodsGuanIaji, putGoodsGuanIaji } from "@/api";
export default {
  created() {
    this.initData();
  },
  data() {
    return {
      isShowEditTk: false,

      tableData: [],

      //搜索数据
      val: "",

      //日期
      data: [],

      //表单编辑数据
      ruleForm: {
        type_name: ""
      },
      rules: {
        //键 ruleForm里面的键
        //值 数组里面是一个个对象 赋值
        type_name: [
          { required: true, message: "请输入类型名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //请求接口渲染页面
    initData() {
      getGoodsGuanIaji().then(res => {
        // console.log(res);
        this.tableData = res.data.list;
      });
    },

    //搜索功能,记得传参
    searchFn() {
      getGoodsGuanIaji({
        pagenum: this.ruleForm.pagenum,
        pagesize: this.ruleForm.pagesize,
        start_time: this.ruleForm.start_time,
        end_time: this.ruleForm.end_time,
        goods_name: this.val,
        is_delete: this.ruleForm.is_delete
      }).then(res => {
        this.tableData = res.data.list;
      });
    },

    //点击开关按钮---刷新按钮效果不变
    changeFn(state, row, type) {
      // console.log(row)  是整个当前行数据
      putGoodsGuanIaji({
        goods_id: row.goods_id,
        key: type,
        state: state
      }).then(res => {
        if (res.meta.state == 200) {
          this.$message.success(res.meta.msg);
        } else {
          this.$message.error(res.meta.msg);
        }
      });
    },

    //编辑
    editFn(id, row) {
      //显示默认数据显示
      this.ruleForm.type_name = row.catename;
      //显示编辑弹框
      this.isShowEditTk = true;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },

    //删除
    delFn(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    //创建
    createFn() {
      this.$router.push({ path: "/goods/create" });
    }
  }
};
</script>

<style lang="scss" scoped>
.goodsGuanIndex {
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  //搜索
  .search {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .input-with-select {
    width: 300px;
  }
  .el-table .cell {
    display: flex;
  }
}
</style>
